package at.ac.tuwien.dse.health.admin.jdbc

import java.sql.Connection
import scala.Int
import scala.beans.BeanProperty

/** Provides utility methods for executing SQL queryies. */
class JdbcTemplate(@BeanProperty val conn: Connection) {
	/** Executes the prepared SQL statement and returns the [[java.sql.ResultSet]]. */
	def query[T](psh: PreparedStatementHolder) = {
		(psh stmt conn).executeQuery
	}

	/** Executes the prepared SQL statement and returns a list of objects after applying the [[at.ac.tuwien.dse.health.admin.jdbc.RowMapper]]. */
	def query[T](psh: PreparedStatementHolder, rowMapper: RowMapper[T]): List[T] = {
		try rowMapper mapRows query(psh)
		finally psh.close()
	}

	/** Executes the prepared SQL statement and returns the number of rows affected. */
	def update(psh: PreparedStatementHolder): Int = {
		try psh.pstmt.executeUpdate
		finally psh.pstmt.close()
	}
}
